package com.hanrui.android.activity.message.threesupervise;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.hanrui.android.R;
import com.hanrui.android.library.base.ActionBarBaseActivity;
import com.hanrui.android.fragment.message.threesupervise.ThreeSuperviseFragment;
import com.hanrui.android.fragment.message.threesupervise.ThreeSuperviseLcFragment;
import com.hanrui.android.widget.bottomnavigationbar.BottomDoneBar;

import java.util.Stack;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 督办审批基本信息
 */
public class ThreeSuperviseDetailActivity extends ActionBarBaseActivity {

    private Stack<Fragment> stack = new Stack<>();
    @BindView(R.id.contentContainer)
    FrameLayout contentContainer;
    @BindView(R.id.bottomBar)
    BottomDoneBar mBottomNavBar;

    FragmentTransaction manager;
    ThreeSuperviseFragment threeSuperviseFragment;
    ThreeSuperviseLcFragment threeSuperviseLcFragment;

    String id;

    @Override
    protected void initInjector() {}


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_meet_flow);

        unbinder = ButterKnife.bind(this);

        id = getIntent().getStringExtra("id");

        mTitleBar.setTitle("督办审批");

        initFragment();
        initBottomNav();
        changeFragment(0);
    }


    private void initFragment(){

        threeSuperviseFragment = ThreeSuperviseFragment.newInstance(id);
        threeSuperviseLcFragment = ThreeSuperviseLcFragment.newInstance(id);
        manager = getSupportFragmentManager().beginTransaction();
        manager.add(R.id.contentContainer,threeSuperviseFragment);
        manager.add(R.id.contentContainer,threeSuperviseLcFragment);
        manager.commit();

        stack.add(threeSuperviseFragment);
        stack.add(threeSuperviseLcFragment);

    }

    /**
     * 初始化底部导航切换事件
     */
    private void initBottomNav(){
        mBottomNavBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                switch (position) {
                    case 0:
                        mTitleBar.setTitle("督办审批");
                        break;
                    case 1:
                        mTitleBar.setTitle("流程图");
                        break;
                }

                changeFragment(position);
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });
    }


    /*
        切换Tab，切换对应的Fragment
     */
    private void changeFragment(int position) {
        manager = getSupportFragmentManager().beginTransaction();
        for (Fragment fragment: stack){
            manager.hide(fragment);
        }
        manager.show(stack.get(position));
        manager.commit();
    }

    public static void start(Context context,String id){
        Intent intent = new Intent(context,ThreeSuperviseDetailActivity.class);
        intent.putExtra("id",id);
        context.startActivity(intent);


    }
}
